package com.meixiang.fragment.account;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.lzy.okhttputils.model.HttpParams;
import com.meixiang.R;
import com.meixiang.activity.account.manager.BindPhoneActivity;
import com.meixiang.global.Config;
import com.meixiang.http.HttpCallBack;
import com.meixiang.http.HttpUtils;
import com.meixiang.main.BaseFragment;
import com.meixiang.util.KeyBoardUtils;
import com.meixiang.view.CheckEditTextEmptyButton;
import com.meixiang.view.ClearEditText;

import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 绑定手机——绑定确认
 *
 * @author LZRUI
 */
public class ConfirmPhoneFragment extends BaseFragment {

    private static final String PAY_VERIFY = "payVerify";

    @Bind(R.id.edtTxt_phone_num)
    ClearEditText edtTxtPhoneNum; // 手机号码输入框
    @Bind(R.id.btn_confirm)
    CheckEditTextEmptyButton btnConfirm; //  确定按钮

    private IBindPhoneClickListener clickListener;
    private boolean isPayVerify;

    public ConfirmPhoneFragment() {

    }

    public static ConfirmPhoneFragment newInstance(boolean isPayVerify) {
        Bundle bundle = new Bundle();
        bundle.putBoolean(PAY_VERIFY, isPayVerify);
        ConfirmPhoneFragment fragment = new ConfirmPhoneFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof IBindPhoneClickListener) {
            clickListener = (IBindPhoneClickListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement IConfirmPhoneFragmentToNext");
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            isPayVerify = getArguments().getBoolean(PAY_VERIFY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_confirm_phone, container, false);
        ButterKnife.bind(this, rootView);
        super.onCreateView(inflater, container, savedInstanceState);
        return rootView;
    }

    @Override
    protected void initTitle() {

    }

    @Override
    protected void initView() {
        btnConfirm.setEditText(edtTxtPhoneNum);
    }

    @Override
    protected void setData() {
        edtTxtPhoneNum.setText("");
    }

    @Override
    public void onPause() {
        super.onPause();
        edtTxtPhoneNum.setText("");
        KeyBoardUtils.hideSoftInput(edtTxtPhoneNum);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick(R.id.btn_confirm)
    public void onClick(View view) {
        bindNewPhone();
    }

    private void bindNewPhone() {
        String url;
        if (isPayVerify) {
            // 原手机号不能收到信息，通过支付密码验证更改手机号
            url = Config.UPDATE_MOBILE;
        } else {
            url = Config.UPDATE_MOBILE_BY_SMS;
        }
        HttpUtils.post(url, BindPhoneActivity.TAG, new HttpParams("mobile", edtTxtPhoneNum.getText().toString().trim()), new HttpCallBack(activity) {
            @Override
            public void onSucceed(JSONObject result, String code, String message) {
                KeyBoardUtils.hideSoftInput(edtTxtPhoneNum);
                if (isVisible()) {
                    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                }
                if (clickListener != null) {
                    clickListener.onConfirmPhoneFragmentClick();
                }
            }

            @Override
            public void onError(String code, String message) {
                if (isVisible()) {
                    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

}
